/*
 * This program is free software: you can redistribute it and/or modify it under the terms of the 
 * GNU Affero General Public License as published by the Free Software Foundation, either version 3 
 * of the License, or (at your option) any later version.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3, 
 * the Appropriate Legal Notices must display the following words on all interactive user interfaces: 
 * "Copyright (C) 2005-2011 SplendidCRM Software, Inc. All rights reserved."
 */

var Sql =
{
	  ToString: function(o)
	{
		if ( o === undefined || o == null )
			return '';
		return o;
	}
	, ToInteger: function(n)
	{
		if ( n === undefined || n == null )
			return 0;
		return parseInt(n);
	}
	, ToBoolean: function(b)
	{
		if ( b === undefined || b == null )
			return false;
		return (b == 'true' || b == 'on' || b == '1' || b == true || b == 1);
	}
	, EscapeSQL: function(str)
	{
		if ( str != null )
		{
			str.replace('\'', '\'\'');
		}
		return str;
	}
	, EscapeSQLLike: function(str)
	{
		if ( str != null )
		{
			str = str.replace('\\', '\\\\');
			str = str.replace('%' , '\\%');
			str = str.replace('_' , '\\_');
		}
		return str;
	}
	, EscapeJavaScript: function(str)
	{
		if ( str != null )
		{
			str = str.replace('\\', '\\\\');
			str = str.replace('\'', '\\\'');
			str = str.replace('\"', '\\\"');
			str = str.replace('\t', '\\t');
			str = str.replace('\r', '\\r');
			str = str.replace('\n', '\\n');
		}
		return str;
	}
	, EscapeEmail: function(str)
	{
		if ( str != null )
		{
			str = str.replace('&', '&amp;');
			str = str.replace('<', '&lt;');
			str = str.replace('>', '&gt;');
		}
		return str;
	}
	, IsEmptyString: function(str)
	{
		if ( str === undefined || str == null || str == '' )
			return true;
		return false;
	}
	, IsEmptyGuid: function(str)
	{
		if ( str === undefined || str == null || str == '' || str == '00000000-0000-0000-0000-000000000000' )
			return true;
		return false;
	}
	, AppendParameter: function(cmd, sField, oValue, bOrClause)
	{
		var ControlChars = { CrLf: '\r\n' };
		if ( bOrClause === undefined )
			bOrClause = false;
		// http://www.javascriptkit.com/javatutors/determinevar2.shtml
		if ( oValue == null )
		{
			if ( cmd.CommandText.length > 0 )
			{
				if ( bOrClause )
					cmd.CommandText += ' or ';
				else
					cmd.CommandText += ' and ';
			}
			cmd.CommandText += sField + ' is null' + ControlChars.CrLf;
		}
		else if ( typeof(oValue) == 'number' )
		{
			if ( cmd.CommandText.length > 0 )
			{
				if ( bOrClause )
					cmd.CommandText += ' or ';
				else
					cmd.CommandText += ' and ';
			}
			cmd.CommandText += sField + ' = ' + oValue + ControlChars.CrLf;
		}
		else if ( typeof(oValue) == 'boolean' )
		{
			if ( cmd.CommandText.length > 0 )
			{
				if ( bOrClause )
					cmd.CommandText += ' or ';
				else
					cmd.CommandText += ' and ';
			}
			cmd.CommandText += sField + ' = ' + (oValue ? '1' : '0') + ControlChars.CrLf;
		}
		else if ( typeof(oValue) == 'string' )
		{
			if ( cmd.CommandText.length > 0 )
			{
				if ( bOrClause )
					cmd.CommandText += ' or ';
				else
					cmd.CommandText += ' and ';
			}
			if ( oValue.length == 0 )
			{
				cmd.CommandText += sField + ' is null' + ControlChars.CrLf;
			}
			else
			{
				cmd.CommandText += sField + ' = \'' + this.EscapeSQL(oValue) + '\'' + ControlChars.CrLf;
			}
		}
		else if ( typeof(oValue) == 'object' )
		{
			if ( oValue.length )  // Array test. 
			{
				if ( oValue.length > 0 )
				{
					var bIncludeNull = false;
					var sValueList   = '';
					for ( var i = 0; i < oValue.length; i++ )
					{
						if ( oValue[i] == null || oValue[i].length == 0 )
						{
							bIncludeNull = true;
						}
						else
						{
							if ( sValueList.length > 0 )
								sValueList += ', ';
							sValueList += '\'' + this.EscapeSQL(oValue[i]) + '\'';
						}
					}
					if ( cmd.CommandText.length > 0 )
					{
						if ( bOrClause )
							cmd.CommandText += ' or ';
						else
							cmd.CommandText += ' and ';
					}
					if ( sValueList.length > 0 )
					{
						if ( bIncludeNull )
							cmd.CommandText += '(' + sField + ' is null or ' + sField + ' in (' + sValueList + '))' + ControlChars.CrLf;
						else
							cmd.CommandText += sField + ' in (' + sValueList + ')' + ControlChars.CrLf;
					}
					else if ( bIncludeNull )
					{
						cmd.CommandText += sField + ' is null' + ControlChars.CrLf;
					}
				}
			}
		}
	}
};


